package com.example.fw;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.example.obj.ContactObject;
import com.example.obj.ContactSet;
import com.example.obj.GroupObject;
import com.example.obj.GroupSet;

public class HibernateHelper extends HelperBase {

	public HibernateHelper(ApplicationManager manager) {
		super(manager);
	}

	@SuppressWarnings("unchecked")
	public GroupSet getGroups() {
		log.info("Hibernate: Get groups");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();		
		try {
			return new GroupSet((List<GroupObject>) session.createQuery("from GroupObject order by name, id").list());
		} finally{
			t.commit();
		}
	}
	
	@SuppressWarnings("unchecked")
	public ContactSet getContacts() {
		log.info("Hibernate: Get contacts");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();		
		try {
			return new ContactSet((List<ContactObject>) session.createQuery("from ContactObject order by firstname, lastname, id").list());
		} finally{
			t.commit();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<ContactObject> getContactList() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();		
		try {		
			return (List<ContactObject>) session.createQuery("from ContactObject order by firstname, lastname, id").list();
		} finally{
			t.commit();
		}
	}
}
